﻿using System;
using System.Windows.Forms;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.BaseForm.Properties;
using System.Diagnostics;

namespace Slippor.Douban.Radio.BaseForm
{
    public partial class DoubanForm
    {
        #region 托盘

        private void InitialContextMenu()
        {
            contextMenuStripNotify.Opened += (sender, e) =>
                {
                    _contextMenuStripNotifyOpened = true;
                };
            contextMenuStripNotify.Closed += (sender, e) =>
                {
                    _contextMenuStripNotifyOpened = false;
                };
            toolStripMenuItemDel.Enabled = _isPlaying;
            toolStripMenuItemFav.Enabled = _isPlaying;
            toolStripMenuItemNext.Enabled = _isPlaying;
            toolStripMenuItemControl.Text = _isPlaying ? StringResource.Command_Stop : StringResource.Command_Play;
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            //ShowHide();
        }

        int _previousClick = SystemInformation.DoubleClickTime;
        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int now = System.Environment.TickCount;
                if (now - _previousClick > SystemInformation.DoubleClickTime)
                {
                    ShowHide();
                }
                _previousClick = now;
            }
        }

        private void ShowHide()
        {
            if (_currentState == RadioState.Notify)
            {
                _currentState = RadioState.Normal;
                ShowFromNotify();
            }
            else
            {
                HideToNotify();
            }
        }

        private object _forLock = new object();
        protected virtual void HideToNotify()
        {
            _currentState = RadioState.Notify;
            toolStripMenuItemShow.Text = Resources.DoubanForm_Common_Display;
            if (IsTaskbarShow)
            {
                HideTaskBar();
            }
            HideMainWindow();
            if (EnableHotKey)
            {
                lock (_forLock)
                {
                    UnHotkeys();
                    RegHotkeys();
                }
            }
        }

        protected virtual void ShowFromNotify()
        {
            toolStripMenuItemShow.Text = Resources.DoubanForm_Common_Hide;
            if (IsTaskbarShow)
            {
                ShowTaskBar();
            }
            ShowMainWindow();
            if (_currentSong != Song.DefaultSong)
            {
                FreshInfo(0.75);
            }
            if (_currentState == RadioState.Normal)
            {
                Activate();
                BringToFront();
            }
            else
            {
                HideMainWindow();
            }
            if (EnableHotKey)
            {
                lock (_forLock)
                {
                    UnHotkeys();
                    RegHotkeys();
                }
            }
        }

        private void toolStripMenuItemClear_Click(object sender, EventArgs e)
        {
            _toolStripTracker.Clear();
            contextMenuStripNotify.SuspendLayout();
            toolStripMenuItemHistory.DropDownItems.Clear();
            toolStripMenuItemHistory.DropDownItems.Add(toolStripMenuItemClear);
            toolStripMenuItemHistory.DropDownItems.Add(new ToolStripSeparator());
            contextMenuStripNotify.ResumeLayout();
            RadioUtility.ReleaseMemory();
        }

        private void toolStripMenuItemFav_Click(object sender, EventArgs e)
        {
            ExcuteCommand(CommandFactory.GetCommand(Command.Fav, _currentType));
        }

        private void toolStripMenuItemNext_Click(object sender, EventArgs e)
        {
            ExcuteCommand(CommandFactory.GetCommand(Command.Next, _currentType));
        }

        private void toolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            ExcuteCommand(CommandFactory.GetCommand(Command.Del, _currentType));
        }

        private void toolStripMenuItemControl_Click(object sender, EventArgs e)
        {
            ExcuteCommand(CommandFactory.GetCommand(Command.Control, _currentType));
        }

        bool _realClose = false;
        private void toolStripMenuItemClose_Click(object sender, EventArgs e)
        {
            _realClose = true;
            Close();
        }

        private void toolStripMenuItemShow_Click(object sender, EventArgs e)
        {
            ShowHide();
        }

        private void toolStripMenuItemTitle_Click(object sender, EventArgs e)
        {
            contextMenuStripNotify.SuspendLayout();
            toolStripMenuItemSearch.SearchSong = _currentSong;
            contextMenuStripNotify.Show();
            toolStripMenuItemSearch.ShowDropDown();
            contextMenuStripNotify.ResumeLayout();
            CopyInfo(_currentSong.Title);
        }

        private void toolStripMenuItemRefresh_Click(object sender, EventArgs e)
        {
            webBrowserDouban.Navigate(StaticResources.RadioUrl);
        }

        #region 设置

        private ConfigForm _configForm;

        private void toolStripMenuItemPop_Click(object sender, EventArgs e)
        {
            NotifyPop = toolStripMenuItemPop.Checked;
            contextMenuStripNotify.Show();
        }

        private void toolStripMenuItemMostTop_Click(object sender, EventArgs e)
        {
            FormTopMost = toolStripMenuItemMostTop.Checked;
            contextMenuStripNotify.Show();
        }

        private void toolStripMenuItemAdvance_Click(object sender, EventArgs e)
        {
            ShowConfig();
        }

        private void ShowConfig()
        {
            if (_configForm == null || _configForm.IsDisposed)
            {
                _configForm = new ConfigForm { ParentDoubanForm = this };
            }
            _configForm.Show();
            _configForm.Activate();
            _configForm.BringToFront();
        }
        #endregion

        private void toolStripMenuItemLogin_Click(object sender, EventArgs e)
        {
            if (ConfirmLogin())
            {
                if (_currentState == RadioState.Notify)
                {
                    _currentState = RadioState.Normal;
                    ShowFromNotify();
                }
                else if (_currentState == RadioState.Minimized)
                {
                    _currentState = RadioState.Normal;
                    ShowMainWindow();
                }
                else
                {
                    Activate();
                }
            }
        }

        private void toolStripMenuItemRestart_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }
        #endregion

        private void toolStripMenuItemGetInfo_Click(object sender, EventArgs e)
        {
            GetSongInfo();
        }

        private void toolStripMenuItemClearCache_Click(object sender, EventArgs e)
        {
            Process.Start("RunDll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 8");
        }
    }
}